var sfDoubleList =
{
  init: function(id, className)
  {
    form = sfDoubleList.get_current_form(id);
    
    classNameForm = className + '-listened';
    if(!$(form).hasClass(classNameForm)){
      $(form).submit(function(){
        sfDoubleList.submit(className);
      });
      $(form).addClass(classNameForm);
    }
    
    document.getElementById('unassociated_' + id.id).ondblclick = function(){
       sfDoubleList.move('unassociated_' + id.id,id.id);
    }
    
    document.getElementById(id.id).ondblclick = function(){
       sfDoubleList.move(id.id,'unassociated_' + id.id);
    }

  },

  move: function(srcId, destId)
  {
    var src = document.getElementById(srcId);
    var dest = document.getElementById(destId);
    for (var i = 0; i < src.options.length; i++)
    {
      if (src.options[i].selected)
      {
        dest.options[dest.length] = new Option(src.options[i].text, src.options[i].value);
        src.options[i] = null;
        --i;
      }
    }
    /* ajout andré tri double liste */ $('#' + destId).sortOptions(); /* fin */
    
    /* ajout Claire rafraichissement des doubles listes suivant la valeur selectionnee (stats ssr)*/  
    if('function' == typeof(double_list_refresh)){
      double_list_refresh(srcId, destId);
    }
  /* fin */
  },

  submit: function(className)
  {
    $('select[class="' + className + '-selected"] option').attr('selected','selected');
  },

  get_current_form: function(el)
  {
    if ("form" != el.tagName.toLowerCase())
    {
      return sfDoubleList.get_current_form(el.parentNode);
    }
    
    return el;
  }
};

function stripVowelAccent(str)
{
 var rExps=[
 {re:/[\xC0-\xC6]/g, ch:'A'},
 {re:/[\xE0-\xE6]/g, ch:'a'},
 {re:/[\xC8-\xCB]/g, ch:'E'},
 {re:/[\xE8-\xEB]/g, ch:'e'},
 {re:/[\xCC-\xCF]/g, ch:'I'},
 {re:/[\xEC-\xEF]/g, ch:'i'},
 {re:/[\xD2-\xD6]/g, ch:'O'},
 {re:/[\xF2-\xF6]/g, ch:'o'},
 {re:/[\xD9-\xDC]/g, ch:'U'},
 {re:/[\xF9-\xFC]/g, ch:'u'},
 {re:/[\xD1]/g, ch:'N'},
 {re:/[\xF1]/g, ch:'n'} ];

 for(var i=0, len=rExps.length; i<len; i++)
  str=str.replace(rExps[i].re, rExps[i].ch);

 return str;
}

jQuery.fn.filterByText = function(textbox, selectSingleMatch, destId) {
  return this.each(function() {
    var select = this;
    var options = [];
    $(select).find('option').each(function() {
      options.push({
        value: $(this).val(), 
        text: $(this).text()
        });
    });
    $(select).data('options', options);
    $(textbox).bind('change keyup', function() {
      var options = $(select).empty().data('options');
      var search = $.trim($(this).val());
      var regex = new RegExp(search,'gi');
	 
      $.each(options, function(i) {
        var option = options[i];
        var string = stripVowelAccent(option.text);
        if(string.match(regex) !== null || option.text.match(regex) !== null) {
          if(!$(destId).find('option[value="'+option.value+'"]').length)
            $(select).append(
              $('<option>').text(option.text).val(option.value)
            );
        }
      });
      if (selectSingleMatch === true &&
        $(select).children().length === 1) {
        $(select).children().get(0).selected = true;
      }
    });
  });
};